package com.chen.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.dto.RemoveSubjectFromPaperDTO;
import com.chen.entity.ExamPaper;
import com.chen.entity.ExamPaperConnectSubject;
import com.chen.entity.Subject;
import com.chen.mapper.ExamPaperConnectSubjectMapper;
import com.chen.mapper.ExamPaperMapper;
import com.chen.mapper.SubjectMapper;
import com.chen.service.ExamPaperConnectSubjectService;
import com.chen.vo.ESMDTO;
import com.chen.vo.ExamPaperVO;
import com.chen.vo.ExamSubjectMiddleVO;
import com.chen.vo.SubjectVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 试题与试卷链接相关服务
 */
@Service
public class ExamPaperConnectSubjectServiceImpl extends ServiceImpl<ExamPaperConnectSubjectMapper, ExamPaperConnectSubject> implements ExamPaperConnectSubjectService {

    // 试卷
    @Autowired
    private ExamPaperMapper examPaperMapper;

    // 试题
    @Autowired
    private SubjectMapper subjectMapper;



    /**
     * 通过试卷id查询试卷详细信息
     * @param examPaperId
     * @return
     */
    @Override
    public ExamSubjectMiddleVO getSubjectMiddle(Integer examPaperId) {
        ExamSubjectMiddleVO examSubjectMiddleVO = new ExamSubjectMiddleVO();
        if (ObjectUtil.isNotEmpty(examPaperId)) {
            // 1. 查询试卷信息
            ExamPaper examPaper = examPaperMapper.selectById(examPaperId);
            if (ObjectUtil.isNotEmpty(examPaper)) {
                // 组装试卷信息
                examSubjectMiddleVO.setExamPaper(BeanUtil.toBean(examPaper, ExamPaperVO.class));
                List<ExamPaperConnectSubject> examPaperConnectSubjects = baseMapper.findByExamPaperId(examPaperId);
                if (CollectionUtil.isNotEmpty(examPaperConnectSubjects)) {
                    Set<Integer> subjectIds = examPaperConnectSubjects.stream().map(ExamPaperConnectSubject::getSubjectId).collect(Collectors.toSet());
                    List<Subject> subjects = subjectMapper.selectBatchIds(subjectIds);
                    if (CollectionUtil.isNotEmpty(subjects)) {
                        List<SubjectVO> subjectVOS = BeanUtil.copyToList(subjects, SubjectVO.class);
                        examSubjectMiddleVO.setCount(subjectVOS.size());
                        examSubjectMiddleVO.setSubjects(subjectVOS);
                    }
                }
                return examSubjectMiddleVO;
            }
        }
        return null;
    }

    @Override
    public List<ESMDTO> getESM(Integer examPaperId, String courseName, Integer courseId, Integer gradeId) {
        return baseMapper.getESM(examPaperId, courseName, courseId, gradeId);
    }

    /**
     * 移除试题
     * @param dto
     * @return
     */
    @Override
    public Integer removeSubjectFromPaper(RemoveSubjectFromPaperDTO dto) {
        // 试卷id
        Integer examPaperId = dto.getExamPaperId();
        // 试题id
        Integer subjectId = dto.getSubjectId();
        // 分数
        Integer score = dto.getScore();

        // 1. 解绑
        ExamPaperConnectSubject examPaperConnectSubject = baseMapper.getSubjectByExamPaperIdAndSubjectId(examPaperId, subjectId);
        if (ObjectUtil.isNotEmpty(examPaperConnectSubject)) {
            baseMapper.deleteById(examPaperConnectSubject.getId());
            // 修改试卷分数
            examPaperMapper.addScoreById(examPaperId, (score * -1));
            return 1;
        }
        return 0;
    }
}
